//
//  HTTPTool.m
//  BeanTrip
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "HTTPTool.h"
#import "AFNetworking.h"

@implementation HTTPTool
//get请求
+(void)get:(NSString *)url
      body:(id)body
httpResult:(HTTPStyle)style
   success:(void(^)(id result))success
   failure:(void(^)(NSError *error))failure
{
    //1.获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    //2.判断返回的responseObject的数据类型
    switch (style) {
            //NSData
        case HTTP:
        manager.responseSerializer=[AFHTTPResponseSerializer serializer];
            break;
            //JSON类型
        case JSON:
            break;
            //XML类型
        case XML:
        manager.responseSerializer=[AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    [manager.requestSerializer setValue:@"Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B466 @BreadTrip/5.1.1/zh-Hans Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"bt_devid=i_549d4f4fa7c35e63e2b086afadb0e7c7ca827f81fc62de7158a415472ba7dcf65ec66da7; Hm_lpvt_39ccc5353de4e1969b414e80e516d5a7=1435894222; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1435289251,1435540300,1435824769,1435884631; sessionid=f22ce74edcafac62c96a70f4a461fdce; btuid=49321ee80b5411e5b21ec81f66e3fe07; csrftoken=257b2dd844080beb570242cb0cb6330d" forHTTPHeaderField:@"Cookie"];
    
    
    
    
    
    
    
    //3.让管理类调用get请求
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //如果成功返回数据
        if (success) {
            success(responseObject);
        }
 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //如果失败返回error信息
        if (failure) {
            failure(error);
        }
        
    }];
}


//post请求
+(void)post:(NSString *)url
       body:(id)body
 httpResult:(HTTPStyle)style
requestBodyStyle:(RequestBodyStyle)bodyStyle
<<<<<<< HEAD
   httpHead:(NSDictionary *)dic
=======

    httpHead:(NSDictionary *)dic

   httpHead:(NSDictionary *)dic

>>>>>>> 735d3ca01a72db709b74d38b94470d25d68e070d
    success:(void(^)(id result))success
    failure:(void(^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
<<<<<<< HEAD
    //判断网络请求头的字典是否为空
    if (dic) {
        //不为空遍历字典
        for (NSString *key in dic.allKeys) {
            //设置为HTTP请求头
            [manager.requestSerializer setValue:dic[key] forHTTPHeaderField:key];
        }
    }
=======
>>>>>>> 735d3ca01a72db709b74d38b94470d25d68e070d

    //判断网络请求头的字典是否为空
    if (dic) {
        //不为空遍历字典
        for (NSString *key in dic.allKeys) {
            //设置为HTTP请求头
            [manager.requestSerializer setValue:dic[key] forHTTPHeaderField:key];
        }
    }
    //新添加
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];

    /**
     *  判断网络请求头的字典
     */
    if (dic) {
        /**
         *  不为空遍历字典
         */
        for (NSString *key in dic.allKeys) {
            /**
             *  设置为HTTP请求头
             */
            [manager.requestSerializer setValue:dic[key] forHTTPHeaderField:key];
        }
    }

    
    switch (bodyStyle) {
        case StringBody:
            // 请求参数原样传给服务器
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError **error) {
                return parameters;
            }];
            break;
         case JSONBody:
            manager.requestSerializer=[AFJSONRequestSerializer serializer];
            break;
        default:
            break;
    }
    
<<<<<<< HEAD
    //请求类,有了这个该类型才能请求
     [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    
    
=======
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
>>>>>>> 735d3ca01a72db709b74d38b94470d25d68e070d
    
    switch (style) {
        case HTTP:
            manager.responseSerializer=[AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            break;
        case XML:
            manager.responseSerializer=[AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
[manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if (success) {
        success(responseObject);
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if (failure) {
        failure(error);
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertV show];
        [alertV release];
        
    }
}];

}
@end
